using System;
using CKS.EBE.API;
using Microsoft.SharePoint;
using System.IO;
using System.Diagnostics;

namespace CKS.EBE
{
    public class ThemeFeatureReceiver : SPFeatureReceiver
    {
        protected void AddFile(SPFolder oFolder, FileInfo file)
        {
            try
            {
                SPFile oFile = oFolder.Files[file.Name];
            }
            catch
            {
                Stream oStream = File.OpenRead(file.FullName);
                oFolder.Files.Add(file.Name, oStream);
                oStream.Close();
            }

        }

        public void AddMasterPages(SPFolder themesFolder, SPFeatureReceiverProperties properties)
        {
            string[] arThemes = Directory.GetDirectories(properties.Definition.RootDirectory + "\\Themes");

            foreach (string sTheme in arThemes)
            {
                string[] arMasterPages = Directory.GetFiles(sTheme, "*.master");
                foreach (string sFile in arMasterPages)
                {
                    FileInfo file = new FileInfo(sFile);

                    if (file.Exists && file.Directory != null)
                    {
                        SPFolder oThemeFolder;
                        try
                        {
                            oThemeFolder = themesFolder.SubFolders[file.Directory.Name];
                        }
                        catch
                        {
                            oThemeFolder = themesFolder.SubFolders.Add(file.Directory.Name);
                        }

                        AddFile(oThemeFolder, file);
                    }
                }
            }
        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        { 
            try
            {
                SPWeb oWeb = (SPWeb)properties.Feature.Parent;

                SPList oThemesList = null;
                try { oThemesList = oWeb.Lists[Localization.GetListName(Localization.BlogList.Themes, oWeb.Language)]; }
                catch { }

                if (oThemesList == null)
                {
                    Guid id = oWeb.Lists.Add(Localization.GetListName(Localization.BlogList.Themes, oWeb.Language), Localization.GetResource("ListDescription_Themes", oWeb.Language), SPListTemplateType.DocumentLibrary);
                    oThemesList = oWeb.Lists[Localization.GetListName(Localization.BlogList.Themes, oWeb.Language)];
                }

                SPFolder oThemesFolder = oThemesList.RootFolder;

                try { AddMasterPages(oThemesFolder, properties); }
                catch { SPSecurity.RunWithElevatedPrivileges(delegate { AddMasterPages(oThemesFolder, properties); }); }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
                throw ex;
            }
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            
        }
    }
}
